package com.example.conf;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.example.interceptor.MyInterceptor;

@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter{
//	@Override
//	public void addViewControllers(ViewControllerRegistry registry) {
//		
//		registry.addViewController("/login").setViewName("loginPage");
//	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		//create fastJson messageConverter
		FastJsonHttpMessageConverter fastConverter= new FastJsonHttpMessageConverter();
		FastJsonConfig config=new FastJsonConfig();
		config.setSerializerFeatures(
				SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题，默认为false（如果不配置有可能会进入死循环）
				SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
				SerializerFeature.WriteNullListAsEmpty,	//List字段如果为null,输出为[],而非null
				SerializerFeature.WriteNullStringAsEmpty, // 字符类型字段如果为null,输出为"",而非null
				SerializerFeature.WriteNullBooleanAsFalse	//Boolean字段如果为null,输出为false,而非null
				);
		fastConverter.setFastJsonConfig(config);
		converters.add(fastConverter);
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
	}
	
	
}
